package com.example.android.helloactivity;


public class Situation{

	//
	//coordinate of screen
	//
	public class Coordinate {
		int px;
		int py;
		int invalid;
		
		Coordinate(){
			px = 0;
			py = 0;
			invalid = 0;
		}
	}
	//public static final int QIPANSIZE = 19; 
	int color;
	boolean turn;
	int index;
	String name;
	int win;
	Coordinate  history[];
	//1 stand for self 2 stand for rival 0 stand for null
	int situation[][];
	int emptyNum = 0;
	int middleEmpty=0;
	
	public int numOfQizi(int x,int y,int step1,int step2,Situation rival){
		
		int p = 0;
		int q = 0;
		int i = 0;
		int j = 0;
		int ret = 0;
		int gapNumUp=0;
		int gapNumDown=0;
		emptyNum = 0;
		middleEmpty = 0;
        
        for(p=x+step1,q=y+step2;p>=0&&p<=(ChessView.QIPANSIZE-1)&&q>=0&&q<=(ChessView.QIPANSIZE-1)&&situation[p][q]==1;p+=step1,q+=step2);
        if(p>=0&&p<=(ChessView.QIPANSIZE-1)&&q>=0&&q<=(ChessView.QIPANSIZE-1)&&0==situation[p][q]&&0==rival.situation[p][q]){
        	
        	for(p+=step1,q+=step2;p>=0&&p<=(ChessView.QIPANSIZE-1)&&q>=0&&q<=(ChessView.QIPANSIZE-1)&&situation[p][q]==1;p+=step1,q+=step2){
        		
        		gapNumUp++;
        	}
        	
        	if(0 == gapNumUp){
        		
        		p-=step1;
        		q-=step2;
        	}else{
        		
        		middleEmpty+=1;
        	}
        	
        	if(0==gapNumUp || p>=0&&p<=(ChessView.QIPANSIZE-1)&&q>=0&&q<=(ChessView.QIPANSIZE-1)&&0==situation[p][q]&&0==rival.situation[p][q]){
        		
        		emptyNum+=1;
        	}
        }
        for(i=x-step1,j=y-step2;i>=0&&i<=(ChessView.QIPANSIZE-1)&&j>=0&&j<=(ChessView.QIPANSIZE-1)&&situation[i][j]==1;i-=step1,j-=step2);

        if(i>=0&&i<=(ChessView.QIPANSIZE-1)&&j>=0&&j<=(ChessView.QIPANSIZE-1)&&0==situation[i][j]&&0==rival.situation[i][j]){
        	
        	if(middleEmpty == 1){
        		
        		emptyNum+=1;
        	}else{
        		
        		for(i-=step1,j-=step2;i>=0&&i<=(ChessView.QIPANSIZE-1)&&j>=0&&j<=(ChessView.QIPANSIZE-1)&&situation[i][j]==1;i-=step1,j-=step2){
        			
        			gapNumDown+=1;
        		}
        		
            	if(0 == gapNumDown){
            		
            		i+=step1;
            		j+=step2;
            	}else{
            		
            		middleEmpty+=1;
            	}
        		
        		if(0==gapNumDown || i>=0&&i<=(ChessView.QIPANSIZE-1)&&j>=0&&j<=(ChessView.QIPANSIZE-1)&&0==situation[i][j]&&0==rival.situation[i][j]){
        			
        			emptyNum+=1;
        		}
        	}
        }
        if(0 != step1){
        	ret = Math.abs(p-i)-1-middleEmpty;
        }
        if(0 != step2){
        	ret = Math.abs(q-j)-1-middleEmpty;
        }
        return ret;
	}
	
	public int calcValue(int num,int flag){

		if(6<=num+middleEmpty){
			
			return 0;
		}
		if(5==num){

			if(1==flag){
				return 100000000;
			}
			if(0==flag){
				return 50000000;
			}
		}
		
		return (int) ((flag*2+8)*Math.pow(10,(num-emptyNum%2))*(emptyNum==0?0:1)/(middleEmpty+1));
	}
	
	public boolean judgeWin(int row,int col)
	{
	        int left,right,up,down,i,j,p,q;
	        
	        //check left and right
	        for(left=col-1;left>=0&&situation[row][left]==1;left--);
	        for(right=col+1;right<=(ChessView.QIPANSIZE-1)&&situation[row][right]==1;right++);
	        if(right-left-1==5){
	                
	        	return true;
	        }
	        //check up and down
	        for(up=row-1;up>=0&&situation[up][col]==1;up--);
	        for(down=row+1;down<=(ChessView.QIPANSIZE-1)&&situation[down][col]==1;down++);
	        if(down-up-1==5){
	                
	        	return true;
	        }
	        for(i=row-1,j=col-1;i>=0&&j>=0&&situation[i][j]==1;i--,j--);
	        for(p=row+1,q=col+1;p<=(ChessView.QIPANSIZE-1)&&q<=(ChessView.QIPANSIZE-1)&&situation[p][q]==1;p++,q++);
	        if(p-i-1==5){
	                
	        	return true;
	        }
	        for(i=row-1,j=col+1;i>=0&&j<=(ChessView.QIPANSIZE-1)&&situation[i][j]==1;i--,j++);
	        for(p=row+1,q=col-1;p<=(ChessView.QIPANSIZE-1)&&q>=0&&situation[p][q]==1;p++,q--);
	        if(p-i-1==5){
	        	
	            return true;
	        }
	        return false;
	}
	
	public boolean huiQi(){
		
		//if index is 0,then should not huiqi
		if(index <= 0){
			return true;
		}
		situation[history[index-1].px][history[index-1].py] = 0;
		history[index-1].px=0;
		history[index-1].py=0;
		history[index-1].invalid=0;
		index-=1;
		win = 0;
		return true;
	}
	
	public boolean ReStart(){
		
		win = 0;
		index = 0;
		for(int i=0;i<ChessView.QIPANSIZE*ChessView.QIPANSIZE/2+1;i++){
			
			history[i].px=0;
			history[i].py=0;
			history[i].invalid=0;
		}
		for(int i=0;i<ChessView.QIPANSIZE;i++){
			for(int j=0;j<ChessView.QIPANSIZE;j++){
				
				situation[i][j] = 0;
			}
		}
		return true;
	}
	
	//
	//construe
	//
	Situation(int color,boolean turn,String name){
		
		this.color = color;
		this.turn = turn;
		this.name = name;
		win = 0;
		index = 0;
		history = new Coordinate[ChessView.QIPANSIZE*ChessView.QIPANSIZE/2+1];
		for(int i=0;i<ChessView.QIPANSIZE*ChessView.QIPANSIZE/2+1;i++){
			
			history[i] = new Coordinate();
		}
		for(int i = 0;i<ChessView.QIPANSIZE*ChessView.QIPANSIZE/2+1;i++){
		
			history[i] = new Coordinate();
		}
		situation = new int[ChessView.QIPANSIZE][ChessView.QIPANSIZE];
		
		for(int i=0;i<ChessView.QIPANSIZE;i++){
			for(int j=0;j<ChessView.QIPANSIZE;j++){
				
				situation[i][j] = 0;
			}
		}
	}
}